<?php
namespace app\admin\controller;
use app\admin\model\Proinfo as DbProinfo;
use think\Controller;
use app\admin\model\Project as Dbhome;
use app\admin\model\Article;
use app\admin\model\Proinfo;
use app\admin\model\Bdapi;
use think\Session;
/*项目设置*/

class Project extends Controller{

    //首页渲染模板，输出主页路径与主页模板名称
    public function index(){


        $user=Session::get('user_id');

        echo $user;
        if (!empty($user)){
            //判断是否有session
            $Dbhome=new Dbhome();
            $result=$Dbhome->homePath();
            $this->assign("homepath",$result['homepath']);
            $this->assign("filename",$result['filename']);
            $this->assign("art_dir_name",$result['art_dir_name']);
            $this->assign("projectname",$result['projectname']);
            $this->assign("id",$result['id']);
            $this->assign('domain_name',$result['domain_name']);

            return $this->fetch();

        }else{

            $this->redirect('admin/login/index');
        }


    }



    //输出项目信息
    public function show_proinfo(){
        $Dbproinfo=new Proinfo();


        $result=$Dbproinfo->show();

        return json_encode($result);


    }



    //输出百度接口配置信息
    public function show_bd(){
        $DBbdapi=new Bdapi();
        $result=$DBbdapi->show();
        return json_encode($result);
    }


    //百度接口配置修改
    public function bd_editConfig(){
        $id=input("id");
        $token=input("token");//百度站长token

        $DBbdapi=new Bdapi();
        $DBbdapi->save_config($id,$token);
        $fh="保存成功";
        return json_encode($fh);

    }





    //修改项目配置
    public function editConfig(){
        $id=input('id');
        $path=input('homepath');
        $filename=input('filename');
        $projectname=input('projectname');
        $art_dir_name=input("art_dir_name");
        $domain_name=input('domain_name');

        $Dbhome=new Dbhome();
        $Dbhome->updateConfig($id,$path,$filename,$projectname,$art_dir_name,$domain_name);

            $fh=[
                "code"=>1,
                "message"=>"保存成功"
            ];


        return json_encode($fh);
    }


    //修改项目信息
    public function set_proinfo(){
        $id=input('id');
        $name=input('name');
        $icp=input('icp');
        $tj=input('tj');
        $logo_url=input('logo');
        $Dbproinfo=new DbProinfo();
        $Dbproinfo->set_proinfo($id,$name,$icp,$tj,$logo_url);
        $fh="保存成功";

        print_r(json_encode($fh));

    }


    //项目信息logo上传
    public function Upload_logo(){
        $Dbhome=new Dbhome();
        $HomePath=$Dbhome->selectHomePath();//查询根目录路径

        $img=request()->file('file');


        $oldName=$img->getInfo()['name'];
        $temp = explode(".",$oldName);//将旧缩率图名称分割，
        $newname = 'logo'.time().".".$temp[count($temp)-1];


        $year=date("Y");//获取年份
        $month=date("n");//获取月份
        $day=date("j");//获取天数

        if (!File_exists ($HomePath."logo")){
            mkdir($HomePath."logo");//不存在就创建
        }

        $file=$HomePath."logo/".$year.$month.$day."/";//拼接成文件夹

        if (is_dir($file)){
            //如果目录存在那么就将图片移动至文件夹下
            $img->move($file,$newname);
        }else{
            mkdir($file);//创建路径
            $img->move($file,$newname);
        }


        /*
         $thumbnailUrl用于保存至数据库，
        后续前端输出，所以直接保存根目录路径即可，
        无需加$HomePath
         */
        $logo_Url = "/logo/".$year.$month.$day."/".$newname;

        $fh=[
            "code"=>0,
            "msg"=>"",
            "data"=>[
                "src"=>$logo_Url
            ]
        ];

        print_r(json_encode($fh));



    }




    //预览主页模板
    public function Preview(){
//
        $home=new Dbhome();
        $homeResult=$home->homePath();
        $filename=$homeResult['filename'];//获取主页模板名称

        return  $this->fetch("public/static/templets/index/$filename");

    }

    //更新主页
    public function updateHomePage(){
        $home=new Dbhome();
        $homeResult=$home->homePath();
        $path=$homeResult['homepath'];//获取主页路径
        $filename=$homeResult['filename'];//获取主页模板名称

        $this->fetch("public/static/templets/index/$filename");//模板渲染无需数据，数据已在标签库中输出
        $this->buildHtml('index',"$path","public/static/templets/index/$filename");
//        //将动态页面生成为静态页面，
//        //参数1：页面名称
//        //参数2：页面路径
//        //参数3：模板页面路径
        print_r(json_encode("更新完毕"));
    }


    /**
     * 清除runtime下temp缓存文件
     */
    public function cache_clear() {
        $this->deldir(TEMP_PATH);
        return json_encode("缓存已清除");
    }

    /**
     * @param $dir 要删除的路径
     */
    public function deldir($dir) {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    deldir($fullpath);
                }
            }
        }
    }


}
